{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 运行时错误"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tvm\n",
    "from tvm import te\n",
    "import tvm.testing"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 测试 op 翻译"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "ferror = tvm.testing.test_raise_error_callback(\"OpNotImplemented: myop\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "try:\n",
    "    ferror()\n",
    "    assert False\n",
    "except tvm.error.OpNotImplemented as e:\n",
    "    msg = str(e)\n",
    "    assert isinstance(e, NotImplementedError)\n",
    "    assert msg.find(\"ffi_testing.cc\") != -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "fchk_eq = tvm.testing.test_check_eq_callback(\"InternalError: myop\")\n",
    "try:\n",
    "    fchk_eq(0, 1)\n",
    "    assert False\n",
    "except tvm.error.InternalError as e:\n",
    "    msg = str(e)\n",
    "    assert msg.find(\"ffi_testing.cc\") != -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "try:\n",
    "    tvm.testing.ErrorTest(0, 1)\n",
    "    assert False\n",
    "except ValueError as e:\n",
    "    msg = str(e)\n",
    "    assert msg.find(\"ffi_testing.cc\") != -1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 测试深度回调"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "def test_deep_callback():\n",
    "    def error_callback():\n",
    "        raise ValueError(\"callback error\")\n",
    "\n",
    "    wrap1 = tvm.testing.test_wrap_callback(error_callback)\n",
    "\n",
    "    def flevel2():\n",
    "        wrap1()\n",
    "\n",
    "    wrap2 = tvm.testing.test_wrap_callback(flevel2)\n",
    "\n",
    "    def flevel3():\n",
    "        wrap2()\n",
    "\n",
    "    wrap3 = tvm.testing.test_wrap_callback(flevel3)\n",
    "\n",
    "    try:\n",
    "        wrap3()\n",
    "        assert False\n",
    "    except ValueError as e:\n",
    "        msg = str(e)\n",
    "        idx2 = msg.find(\"in flevel2\")\n",
    "        idx3 = msg.find(\"in flevel3\")\n",
    "        assert idx2 != -1 and idx3 != -1\n",
    "        assert idx2 > idx3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_deep_callback()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "ename": "TVMError",
     "evalue": "Traceback (most recent call last):\n  6: TVMFuncCall\n  5: _ZN3tvm7runtime13PackedFu\n  4: tvm::MakeNode(tvm::runtime::TVMArgs const&, tvm::runtime::TVMRetValue*)\n  3: tvm::ReflectionVTable::CreateObject(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, tvm::runtime::TVMArgs const&)\n  2: tvm::InitNodeByPackedArgs(tvm::ReflectionVTable*, tvm::runtime::Object*, tvm::runtime::TVMArgs const&)\n  1: tvm::NodeAttrSetter::Visit(char const*, tvm::runtime::ObjectRef*)\n  0: tvm::NodeAttrSetter::GetAttr(char const*) [clone .part.0]\n  File \"/media/pc/data/lxw/ai/tvm/src/node/reflection.cc\", line 189\nIntImm: require field span",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTVMError\u001b[0m                                  Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[13], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m x \u001b[39m=\u001b[39m tvm\u001b[39m.\u001b[39;49mir\u001b[39m.\u001b[39;49mmake_node(\u001b[39m\"\u001b[39;49m\u001b[39mIntImm\u001b[39;49m\u001b[39m\"\u001b[39;49m, dtype\u001b[39m=\u001b[39;49m\u001b[39m\"\u001b[39;49m\u001b[39mint32\u001b[39;49m\u001b[39m\"\u001b[39;49m, value\u001b[39m=\u001b[39;49m\u001b[39m10\u001b[39;49m)\n\u001b[1;32m      2\u001b[0m \u001b[39massert\u001b[39;00m \u001b[39misinstance\u001b[39m(x, tvm\u001b[39m.\u001b[39mtir\u001b[39m.\u001b[39mIntImm)\n\u001b[1;32m      3\u001b[0m \u001b[39massert\u001b[39;00m x\u001b[39m.\u001b[39mvalue \u001b[39m==\u001b[39m \u001b[39m10\u001b[39m\n",
      "File \u001b[0;32m/media/pc/data/lxw/ai/tvm/xinetzone/__pypackages__/3.10/lib/tvm/ir/attrs.py:163\u001b[0m, in \u001b[0;36mmake_node\u001b[0;34m(type_key, **kwargs)\u001b[0m\n\u001b[1;32m    161\u001b[0m \u001b[39mfor\u001b[39;00m k, v \u001b[39min\u001b[39;00m kwargs\u001b[39m.\u001b[39mitems():\n\u001b[1;32m    162\u001b[0m     args \u001b[39m+\u001b[39m\u001b[39m=\u001b[39m [k, v]\n\u001b[0;32m--> 163\u001b[0m \u001b[39mreturn\u001b[39;00m tvm\u001b[39m.\u001b[39;49mruntime\u001b[39m.\u001b[39;49m_ffi_node_api\u001b[39m.\u001b[39;49mMakeNode(\u001b[39m*\u001b[39;49margs)\n",
      "File \u001b[0;32m/media/pc/data/lxw/ai/tvm/xinetzone/__pypackages__/3.10/lib/tvm/_ffi/_ctypes/packed_func.py:238\u001b[0m, in \u001b[0;36mPackedFuncBase.__call__\u001b[0;34m(self, *args)\u001b[0m\n\u001b[1;32m    226\u001b[0m ret_tcode \u001b[39m=\u001b[39m ctypes\u001b[39m.\u001b[39mc_int()\n\u001b[1;32m    227\u001b[0m \u001b[39mif\u001b[39;00m (\n\u001b[1;32m    228\u001b[0m     _LIB\u001b[39m.\u001b[39mTVMFuncCall(\n\u001b[1;32m    229\u001b[0m         \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mhandle,\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m    236\u001b[0m     \u001b[39m!=\u001b[39m \u001b[39m0\u001b[39m\n\u001b[1;32m    237\u001b[0m ):\n\u001b[0;32m--> 238\u001b[0m     \u001b[39mraise\u001b[39;00m get_last_ffi_error()\n\u001b[1;32m    239\u001b[0m _ \u001b[39m=\u001b[39m temp_args\n\u001b[1;32m    240\u001b[0m _ \u001b[39m=\u001b[39m args\n",
      "\u001b[0;31mTVMError\u001b[0m: Traceback (most recent call last):\n  6: TVMFuncCall\n  5: _ZN3tvm7runtime13PackedFu\n  4: tvm::MakeNode(tvm::runtime::TVMArgs const&, tvm::runtime::TVMRetValue*)\n  3: tvm::ReflectionVTable::CreateObject(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, tvm::runtime::TVMArgs const&)\n  2: tvm::InitNodeByPackedArgs(tvm::ReflectionVTable*, tvm::runtime::Object*, tvm::runtime::TVMArgs const&)\n  1: tvm::NodeAttrSetter::Visit(char const*, tvm::runtime::ObjectRef*)\n  0: tvm::NodeAttrSetter::GetAttr(char const*) [clone .part.0]\n  File \"/media/pc/data/lxw/ai/tvm/src/node/reflection.cc\", line 189\nIntImm: require field span"
     ]
    }
   ],
   "source": [
    "x = tvm.ir.make_node(\"IntImm\", dtype=\"int32\", value=10)\n",
    "assert isinstance(x, tvm.tir.IntImm)\n",
    "assert x.value == 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[0;31mSignature:\u001b[0m \u001b[0mtvm\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mir\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmake_node\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtype_key\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mDocstring:\u001b[0m\n",
      "Make a new IR node by its type key and fields\n",
      "\n",
      "Parameters\n",
      "----------\n",
      "type_key : str\n",
      "    The type key of the node.\n",
      "\n",
      "**kwargs : dict\n",
      "    The fields of the node.\n",
      "\n",
      "Returns\n",
      "-------\n",
      "node : Node\n",
      "    The corresponding IR Node\n",
      "\n",
      "Note\n",
      "----\n",
      "If the created node is instance of AttrsNode, then\n",
      "the creator function will also run bound checks and\n",
      "default value setup as supported by Attrs.\n",
      "\n",
      "Example\n",
      "-------\n",
      "The following code constructs a IntImm object\n",
      "\n",
      ".. code-block:: python\n",
      "\n",
      "   x = tvm.ir.make_node(\"IntImm\", dtype=\"int32\", value=10)\n",
      "   assert isinstance(x, tvm.tir.IntImm)\n",
      "   assert x.value == 10\n",
      "\u001b[0;31mFile:\u001b[0m      /media/pc/data/lxw/ai/tvm/xinetzone/__pypackages__/3.10/lib/tvm/ir/attrs.py\n",
      "\u001b[0;31mType:\u001b[0m      function"
     ]
    }
   ],
   "source": [
    "x = tvm.ir.make_node?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "tvmz",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
